/*
  Table B5: results with different metrics that are not APG
*/ 

local PATH_TBL = "~/Downloads/tables"

  /* STATE PARTISAN RACES */
  use wide_state_partisan_races, clear
  
  gen D_pct = 100 * D_choice/(D_choice + R_choice)
  gen competitive = (D_pct > 5 & D_pct < 95)
  keep if competitive == 1
  
  // 1: only top-two candidates M = 2
  gen D_D_pid_1 = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen R_D_pid_1 = R_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_R_pid_1 = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen R_R_pid_1 = R_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)

  // _2: top-two plus roll-=ff
  gen D_D_pid_2 = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen R_D_pid_2 = R_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen D_R_pid_2 = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)
  gen R_R_pid_2 = R_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)

  // _3: all candidates (X1 - X3 is 0 if D/R/A is non-zero)
  gen denom_D_pid_3 = D_choice_D_pid + R_choice_D_pid + U_choice_D_pid
  gen denom_R_pid_3 = D_choice_R_pid + R_choice_R_pid + U_choice_R_pid
  forvalues i = 1(1)11 {
    replace denom_D_pid_3 = denom_D_pid_3 + X`i'_choice_D_pid
    replace denom_R_pid_3 = denom_R_pid_3 + X`i'_choice_R_pid
  }
  gen D_D_pid_3 = D_choice_D_pid/denom_D_pid_3
  gen R_D_pid_3 = R_choice_D_pid/denom_D_pid_3
  gen U_D_pid_3 = U_choice_D_pid/denom_D_pid_3
  gen D_R_pid_3 = D_choice_R_pid/denom_R_pid_3
  gen R_R_pid_3 = R_choice_R_pid/denom_R_pid_3
  gen U_R_pid_3 = U_choice_R_pid/denom_R_pid_3
  forvalues i = 1(1)11 {
    gen X`i'_D_pid_3 = X`i'_choice_D_pid/denom_D_pid_3
    gen X`i'_R_pid_3 = X`i'_choice_D_pid/denom_R_pid_3
  }

  // start to caluclate measures
  
  // APD: Absolute Partisan Difference
  gen diff_abs_1 = abs(D_D_pid_1 - D_R_pid_1)/2 + abs(R_D_pid_1 - R_R_pid_1)/2
  gen diff_abs_2 = abs(D_D_pid_2 - D_R_pid_2)/2 + abs(R_D_pid_2 - R_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2
  gen diff_abs_3 = abs(D_D_pid_3 - D_R_pid_3)/2 + abs(R_D_pid_3 - R_R_pid_3)/2 + abs(U_D_pid_3 - U_R_pid_3)/2
  forvalues i = 1(1)11 {
    replace diff_abs_3 = diff_abs_3 + abs(X`i'_D_pid_3 - X`i'_R_pid_3)/2 
  }

  sum diff_abs_1
  local diff_abs_1_spo = r(mean)
  sum diff_abs_2
  local diff_abs_2_spo = r(mean)
  sum diff_abs_3
  local diff_abs_3_spo = r(mean)

  // SPD: Average Squared Difference
  gen diff_sq_1 = (D_D_pid_1 - D_R_pid_1)^2/2 + (R_D_pid_1 - R_R_pid_1)^2/2
  gen diff_sq_2 = (D_D_pid_2 - D_R_pid_2)^2/2 + (R_D_pid_2 - R_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  gen diff_sq_3 = (D_D_pid_3 - D_R_pid_3)^2/2 + (R_D_pid_3 - R_R_pid_3)^2/2 + (U_D_pid_3 - U_R_pid_3)^2/2
  forvalues i = 1(1)11 {
    replace diff_sq_3 = diff_sq_3 + (X`i'_D_pid_3 - X`i'_R_pid_3)^2/2 
  }
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)
  replace diff_sq_3 = sqrt(diff_sq_3)

  sum diff_sq_1
  local diff_sq_1_spo = r(mean)
  sum diff_sq_2
  local diff_sq_2_spo = r(mean)
  sum diff_sq_3
  local diff_sq_3_spo = r(mean)

  // NPDC: Normalized Probability of Different Choices
  // normalizing constant \sum_{m=1}^M [(P^D_{jm} + P^R_{jm})/2]^2
  gen norm_1 = (D_D_pid_1 + D_R_pid_1)^2/4 + (R_D_pid_1 + R_R_pid_1)^2/4
  gen norm_2 = (D_D_pid_2 + D_R_pid_2)^2/4 + (R_D_pid_2 + R_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4
  gen norm_3 = (D_D_pid_3 + D_R_pid_3)^2/4 + (R_D_pid_3 + R_R_pid_3)^2/4 + (U_D_pid_3 + U_R_pid_3)^2/4
  forvalues i = 1(1)11 {
    replace norm_3 = norm_3 + (X`i'_D_pid_3 + X`i'_R_pid_3)^2/4
  }
  gen P_diff_1 = 1 - (D_D_pid_1 * D_R_pid_1 + R_D_pid_1 * R_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (D_D_pid_2 * D_R_pid_2 + R_D_pid_2 * R_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2
  gen P_same_3 = (D_D_pid_3 * D_R_pid_3 + R_D_pid_3 * R_R_pid_3 + U_D_pid_3 * U_R_pid_3) 
  forvalues i = 1(1)11 {
    replace P_same_3 = P_same_3 + X`i'_D_pid_3 * X`i'_R_pid_3 
  }
  gen P_diff_3 = 1 - P_same_3/norm_3

  sum P_diff_1
  local P_diff_1_spo = r(mean)
  sum P_diff_2
  local P_diff_2_spo = r(mean)
  sum P_diff_3
  local P_diff_3_spo = r(mean)
  
  
  /* LOCAL PARTISAN */
  use wide_local_partisan_races, clear
  
  gen D_pct = 100 * D_choice/(D_choice + R_choice)
  gen competitive = (D_pct > 5 & D_pct < 95)
  keep if competitive == 1
  
  gen D_D_pid_1 = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen R_D_pid_1 = R_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_R_pid_1 = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen R_R_pid_1 = R_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)

  gen D_D_pid_2 = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen R_D_pid_2 = R_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(D_choice_D_pid + R_choice_D_pid + U_choice_D_pid)
  gen D_R_pid_2 = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)
  gen R_R_pid_2 = R_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(D_choice_R_pid + R_choice_R_pid + U_choice_R_pid)

  gen denom_D_pid_3 = D_choice_D_pid + R_choice_D_pid + U_choice_D_pid
  gen denom_R_pid_3 = D_choice_R_pid + R_choice_R_pid + U_choice_R_pid
  forvalues i = 1(1)11 {
    replace denom_D_pid_3 = denom_D_pid_3 + X`i'_choice_D_pid
    replace denom_R_pid_3 = denom_R_pid_3 + X`i'_choice_R_pid
  }
  gen D_D_pid_3 = D_choice_D_pid/denom_D_pid_3
  gen R_D_pid_3 = R_choice_D_pid/denom_D_pid_3
  gen U_D_pid_3 = U_choice_D_pid/denom_D_pid_3
  gen D_R_pid_3 = D_choice_R_pid/denom_R_pid_3
  gen R_R_pid_3 = R_choice_R_pid/denom_R_pid_3
  gen U_R_pid_3 = U_choice_R_pid/denom_R_pid_3
  forvalues i = 1(1)11 {
    gen X`i'_D_pid_3 = X`i'_choice_D_pid/denom_D_pid_3
    gen X`i'_R_pid_3 = X`i'_choice_D_pid/denom_R_pid_3
  }

  gen diff_abs_1 = abs(D_D_pid_1 - D_R_pid_1)/2 + abs(R_D_pid_1 - R_R_pid_1)/2
  gen diff_abs_2 = abs(D_D_pid_2 - D_R_pid_2)/2 + abs(R_D_pid_2 - R_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2
  gen diff_abs_3 = abs(D_D_pid_3 - D_R_pid_3)/2 + abs(R_D_pid_3 - R_R_pid_3)/2 + abs(U_D_pid_3 - U_R_pid_3)/2
  forvalues i = 1(1)11 {
    replace diff_abs_3 = diff_abs_3 + abs(X`i'_D_pid_3 - X`i'_R_pid_3)/2 
  }

  sum diff_abs_1
  local diff_abs_1_lpo = r(mean)
  sum diff_abs_2
  local diff_abs_2_lpo = r(mean)
  sum diff_abs_3
  local diff_abs_3_lpo = r(mean)

  gen diff_sq_1 = (D_D_pid_1 - D_R_pid_1)^2/2 + (R_D_pid_1 - R_R_pid_1)^2/2
  gen diff_sq_2 = (D_D_pid_2 - D_R_pid_2)^2/2 + (R_D_pid_2 - R_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  gen diff_sq_3 = (D_D_pid_3 - D_R_pid_3)^2/2 + (R_D_pid_3 - R_R_pid_3)^2/2 + (U_D_pid_3 - U_R_pid_3)^2/2
  forvalues i = 1(1)11 {
    replace diff_sq_3 = diff_sq_3 + (X`i'_D_pid_3 - X`i'_R_pid_3)^2/2 
  }
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)
  replace diff_sq_3 = sqrt(diff_sq_3)

  sum diff_sq_1
  local diff_sq_1_lpo = r(mean)
  sum diff_sq_2
  local diff_sq_2_lpo = r(mean)
  sum diff_sq_3
  local diff_sq_3_lpo = r(mean)

  gen norm_1 = (D_D_pid_1 + D_R_pid_1)^2/4 + (R_D_pid_1 + R_R_pid_1)^2/4
  gen norm_2 = (D_D_pid_2 + D_R_pid_2)^2/4 + (R_D_pid_2 + R_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4
  gen norm_3 = (D_D_pid_3 + D_R_pid_3)^2/4 + (R_D_pid_3 + R_R_pid_3)^2/4 + (U_D_pid_3 + U_R_pid_3)^2/4
  forvalues i = 1(1)11 {
    replace norm_3 = norm_3 + (X`i'_D_pid_3 + X`i'_R_pid_3)^2/4
  }
  gen P_diff_1 = 1 - (D_D_pid_1 * D_R_pid_1 + R_D_pid_1 * R_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (D_D_pid_2 * D_R_pid_2 + R_D_pid_2 * R_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2
  gen P_same_3 = (D_D_pid_3 * D_R_pid_3 + R_D_pid_3 * R_R_pid_3 + U_D_pid_3 * U_R_pid_3) 
  forvalues i = 1(1)11 {
    replace P_same_3 = P_same_3 + X`i'_D_pid_3 * X`i'_R_pid_3 
  }
  gen P_diff_3 = 1 - P_same_3/norm_3

  sum P_diff_1
  local P_diff_1_lpo = r(mean)
  sum P_diff_2
  local P_diff_2_lpo = r(mean)
  sum P_diff_3
  local P_diff_3_lpo = r(mean)
  
  
  
  /* STATE NONPARTISAN RACES */
  use wide_state_nonpartisan_races, clear
  
  gen A_pct = 100 * A_choice/(A_choice + B_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  keep if competitive == 1
  
  gen A_D_pid_1 = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen B_D_pid_1 = B_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_R_pid_1 = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen B_R_pid_1 = B_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)

  gen A_D_pid_2 = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen B_D_pid_2 = B_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen A_R_pid_2 = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)
  gen B_R_pid_2 = B_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)

  gen denom_D_pid_3 = A_choice_D_pid + B_choice_D_pid + U_choice_D_pid
  gen denom_R_pid_3 = A_choice_R_pid + B_choice_R_pid + U_choice_R_pid
  forvalues i = 1(1)11 {
    replace denom_D_pid_3 = denom_D_pid_3 + X`i'_choice_D_pid
    replace denom_R_pid_3 = denom_R_pid_3 + X`i'_choice_R_pid
  }
  gen A_D_pid_3 = A_choice_D_pid/denom_D_pid_3
  gen B_D_pid_3 = B_choice_D_pid/denom_D_pid_3
  gen U_D_pid_3 = U_choice_D_pid/denom_D_pid_3
  gen A_R_pid_3 = A_choice_R_pid/denom_R_pid_3
  gen B_R_pid_3 = B_choice_R_pid/denom_R_pid_3
  gen U_R_pid_3 = U_choice_R_pid/denom_R_pid_3
  forvalues i = 1(1)11 {
    gen X`i'_D_pid_3 = X`i'_choice_D_pid/denom_D_pid_3
    gen X`i'_R_pid_3 = X`i'_choice_D_pid/denom_R_pid_3
  }

  gen diff_abs_1 = abs(A_D_pid_1 - A_R_pid_1)/2 + abs(B_D_pid_1 - B_R_pid_1)/2
  gen diff_abs_2 = abs(A_D_pid_2 - A_R_pid_2)/2 + abs(B_D_pid_2 - B_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2
  gen diff_abs_3 = abs(A_D_pid_3 - A_R_pid_3)/2 + abs(B_D_pid_3 - B_R_pid_3)/2 + abs(U_D_pid_3 - U_R_pid_3)/2
  forvalues i = 1(1)11 {
    replace diff_abs_3 = diff_abs_3 + abs(X`i'_D_pid_3 - X`i'_R_pid_3)/2 
  }

  sum diff_abs_1
  local diff_abs_1_sno = r(mean)
  sum diff_abs_2
  local diff_abs_2_sno = r(mean)
  sum diff_abs_3
  local diff_abs_3_sno = r(mean)

  gen diff_sq_1 = (A_D_pid_1 - A_R_pid_1)^2/2 + (B_D_pid_1 - B_R_pid_1)^2/2
  gen diff_sq_2 = (A_D_pid_2 - A_R_pid_2)^2/2 + (B_D_pid_2 - B_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  gen diff_sq_3 = (A_D_pid_3 - A_R_pid_3)^2/2 + (B_D_pid_3 - B_R_pid_3)^2/2 + (U_D_pid_3 - U_R_pid_3)^2/2
  forvalues i = 1(1)11 {
    replace diff_sq_3 = diff_sq_3 + (X`i'_D_pid_3 - X`i'_R_pid_3)^2/2 
  }
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)
  replace diff_sq_3 = sqrt(diff_sq_3)

  sum diff_sq_1
  local diff_sq_1_sno = r(mean)
  sum diff_sq_2
  local diff_sq_2_sno = r(mean)
  sum diff_sq_3
  local diff_sq_3_sno = r(mean)

  gen norm_1 = (A_D_pid_1 + A_R_pid_1)^2/4 + (B_D_pid_1 + B_R_pid_1)^2/4
  gen norm_2 = (A_D_pid_2 + A_R_pid_2)^2/4 + (B_D_pid_2 + B_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4
  gen norm_3 = (A_D_pid_3 + A_R_pid_3)^2/4 + (B_D_pid_3 + B_R_pid_3)^2/4 + (U_D_pid_3 + U_R_pid_3)^2/4
  forvalues i = 1(1)11 {
    replace norm_3 = norm_3 + (X`i'_D_pid_3 + X`i'_R_pid_3)^2/4
  }
  gen P_diff_1 = 1 - (A_D_pid_1 * A_R_pid_1 + B_D_pid_1 * B_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (A_D_pid_2 * A_R_pid_2 + B_D_pid_2 * B_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2
  gen P_same_3 = (A_D_pid_3 * A_R_pid_3 + B_D_pid_3 * B_R_pid_3 + U_D_pid_3 * U_R_pid_3) 
  forvalues i = 1(1)11 {
    replace P_same_3 = P_same_3 + X`i'_D_pid_3 * X`i'_R_pid_3 
  }
  gen P_diff_3 = 1 - P_same_3/norm_3

  sum P_diff_1
  local P_diff_1_sno = r(mean)
  sum P_diff_2
  local P_diff_2_sno = r(mean)
  sum P_diff_3
  local P_diff_3_sno = r(mean)

  
  /* LOCAL NONPARTISAN RACES */
  use wide_local_nonpartisan_races, clear
  
  gen A_pct = 100 * A_choice/(A_choice + B_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  keep if competitive == 1
  
  gen A_D_pid_1 = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen B_D_pid_1 = B_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_R_pid_1 = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen B_R_pid_1 = B_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)

  gen A_D_pid_2 = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen B_D_pid_2 = B_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(A_choice_D_pid + B_choice_D_pid + U_choice_D_pid)
  gen A_R_pid_2 = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)
  gen B_R_pid_2 = B_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(A_choice_R_pid + B_choice_R_pid + U_choice_R_pid)

  gen denom_D_pid_3 = A_choice_D_pid + B_choice_D_pid + U_choice_D_pid
  gen denom_R_pid_3 = A_choice_R_pid + B_choice_R_pid + U_choice_R_pid
  forvalues i = 1(1)11 {
    replace denom_D_pid_3 = denom_D_pid_3 + X`i'_choice_D_pid
    replace denom_R_pid_3 = denom_R_pid_3 + X`i'_choice_R_pid
  }
  gen A_D_pid_3 = A_choice_D_pid/denom_D_pid_3
  gen B_D_pid_3 = B_choice_D_pid/denom_D_pid_3
  gen U_D_pid_3 = U_choice_D_pid/denom_D_pid_3
  gen A_R_pid_3 = A_choice_R_pid/denom_R_pid_3
  gen B_R_pid_3 = B_choice_R_pid/denom_R_pid_3
  gen U_R_pid_3 = U_choice_R_pid/denom_R_pid_3
  forvalues i = 1(1)11 {
    gen X`i'_D_pid_3 = X`i'_choice_D_pid/denom_D_pid_3
    gen X`i'_R_pid_3 = X`i'_choice_D_pid/denom_R_pid_3
  }

  gen diff_abs_1 = abs(A_D_pid_1 - A_R_pid_1)/2 + abs(B_D_pid_1 - B_R_pid_1)/2
  gen diff_abs_2 = abs(A_D_pid_2 - A_R_pid_2)/2 + abs(B_D_pid_2 - B_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2
  gen diff_abs_3 = abs(A_D_pid_3 - A_R_pid_3)/2 + abs(B_D_pid_3 - B_R_pid_3)/2 + abs(U_D_pid_3 - U_R_pid_3)/2
  forvalues i = 1(1)11 {
    replace diff_abs_3 = diff_abs_3 + abs(X`i'_D_pid_3 - X`i'_R_pid_3)/2 
  }

  sum diff_abs_1
  local diff_abs_1_lno = r(mean)
  sum diff_abs_2
  local diff_abs_2_lno = r(mean)
  sum diff_abs_3
  local diff_abs_3_lno = r(mean)

  gen diff_sq_1 = (A_D_pid_1 - A_R_pid_1)^2/2 + (B_D_pid_1 - B_R_pid_1)^2/2
  gen diff_sq_2 = (A_D_pid_2 - A_R_pid_2)^2/2 + (B_D_pid_2 - B_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  gen diff_sq_3 = (A_D_pid_3 - A_R_pid_3)^2/2 + (B_D_pid_3 - B_R_pid_3)^2/2 + (U_D_pid_3 - U_R_pid_3)^2/2
  forvalues i = 1(1)11 {
    replace diff_sq_3 = diff_sq_3 + (X`i'_D_pid_3 - X`i'_R_pid_3)^2/2 
  }
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)
  replace diff_sq_3 = sqrt(diff_sq_3)

  sum diff_sq_1
  local diff_sq_1_lno = r(mean)
  sum diff_sq_2
  local diff_sq_2_lno = r(mean)
  sum diff_sq_3
  local diff_sq_3_lno = r(mean)

  gen norm_1 = (A_D_pid_1 + A_R_pid_1)^2/4 + (B_D_pid_1 + B_R_pid_1)^2/4
  gen norm_2 = (A_D_pid_2 + A_R_pid_2)^2/4 + (B_D_pid_2 + B_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4
  gen norm_3 = (A_D_pid_3 + A_R_pid_3)^2/4 + (B_D_pid_3 + B_R_pid_3)^2/4 + (U_D_pid_3 + U_R_pid_3)^2/4
  forvalues i = 1(1)11 {
    replace norm_3 = norm_3 + (X`i'_D_pid_3 + X`i'_R_pid_3)^2/4
  }
  gen P_diff_1 = 1 - (A_D_pid_1 * A_R_pid_1 + B_D_pid_1 * B_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (A_D_pid_2 * A_R_pid_2 + B_D_pid_2 * B_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2
  gen P_same_3 = (A_D_pid_3 * A_R_pid_3 + B_D_pid_3 * B_R_pid_3 + U_D_pid_3 * U_R_pid_3) 
  forvalues i = 1(1)11 {
    replace P_same_3 = P_same_3 + X`i'_D_pid_3 * X`i'_R_pid_3 
  }
  gen P_diff_3 = 1 - P_same_3/norm_3

  sum P_diff_1
  local P_diff_1_lno = r(mean)
  sum P_diff_2
  local P_diff_2_lno = r(mean)
  sum P_diff_3
  local P_diff_3_lno = r(mean)
  
  
  /* STATE BALLOT MEASURES */
  use wide_state_ballot_measures, clear
  
  gen Y_pct = 100 * Y_choice/(Y_choice + N_choice)
  gen competitive = (Y_pct > 5 & Y_pct < 95)
  keep if competitive == 1
  
  gen Y_D_pid_1 = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen N_D_pid_1 = N_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_R_pid_1 = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen N_R_pid_1 = N_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)

  gen Y_D_pid_2 = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen N_D_pid_2 = N_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen Y_R_pid_2 = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)
  gen N_R_pid_2 = N_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)

  gen diff_abs_1 = abs(Y_D_pid_1 - Y_R_pid_1)/2 + abs(N_D_pid_1 - N_R_pid_1)/2
  gen diff_abs_2 = abs(Y_D_pid_2 - Y_R_pid_2)/2 + abs(N_D_pid_2 - N_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2

  sum diff_abs_1
  local diff_abs_1_sbm = r(mean)
  sum diff_abs_2
  local diff_abs_2_sbm = r(mean)

  gen diff_sq_1 = (Y_D_pid_1 - Y_R_pid_1)^2/2 + (N_D_pid_1 - N_R_pid_1)^2/2
  gen diff_sq_2 = (Y_D_pid_2 - Y_R_pid_2)^2/2 + (N_D_pid_2 - N_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)

  sum diff_sq_1
  local diff_sq_1_sbm = r(mean)
  sum diff_sq_2
  local diff_sq_2_sbm = r(mean)

  gen norm_1 = (Y_D_pid_1 + Y_R_pid_1)^2/4 + (N_D_pid_1 + N_R_pid_1)^2/4
  gen norm_2 = (Y_D_pid_2 + Y_R_pid_2)^2/4 + (N_D_pid_2 + N_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4

  gen P_diff_1 = 1 - (Y_D_pid_1 * Y_R_pid_1 + N_D_pid_1 * N_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (Y_D_pid_2 * Y_R_pid_2 + N_D_pid_2 * N_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2

  sum P_diff_1
  local P_diff_1_sbm = r(mean)
  sum P_diff_2
  local P_diff_2_sbm = r(mean)
  
  
  /* LOCAL BALLOT MEASURES */
  use wide_local_ballot_measures, clear
  
  gen Y_D_pid_1 = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen N_D_pid_1 = N_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_R_pid_1 = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen N_R_pid_1 = N_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)

  gen Y_D_pid_2 = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen N_D_pid_2 = N_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen U_D_pid_2 = U_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid + U_choice_D_pid)
  gen Y_R_pid_2 = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)
  gen N_R_pid_2 = N_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)
  gen U_R_pid_2 = U_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid + U_choice_R_pid)

  gen diff_abs_1 = abs(Y_D_pid_1 - Y_R_pid_1)/2 + abs(N_D_pid_1 - N_R_pid_1)/2
  gen diff_abs_2 = abs(Y_D_pid_2 - Y_R_pid_2)/2 + abs(N_D_pid_2 - N_R_pid_2)/2 + abs(U_D_pid_2 - U_R_pid_2)/2

  sum diff_abs_1
  local diff_abs_1_lbm = r(mean)
  sum diff_abs_2
  local diff_abs_2_lbm = r(mean)

  gen diff_sq_1 = (Y_D_pid_1 - Y_R_pid_1)^2/2 + (N_D_pid_1 - N_R_pid_1)^2/2
  gen diff_sq_2 = (Y_D_pid_2 - Y_R_pid_2)^2/2 + (N_D_pid_2 - N_R_pid_2)^2/2 + (U_D_pid_2 - U_R_pid_2)^2/2
  replace diff_sq_1 = sqrt(diff_sq_1)
  replace diff_sq_2 = sqrt(diff_sq_2)

  sum diff_sq_1
  local diff_sq_1_lbm = r(mean)
  sum diff_sq_2
  local diff_sq_2_lbm = r(mean)

  gen norm_1 = (Y_D_pid_1 + Y_R_pid_1)^2/4 + (N_D_pid_1 + N_R_pid_1)^2/4
  gen norm_2 = (Y_D_pid_2 + Y_R_pid_2)^2/4 + (N_D_pid_2 + N_R_pid_2)^2/4 + (U_D_pid_2 + U_R_pid_2)^2/4

  gen P_diff_1 = 1 - (Y_D_pid_1 * Y_R_pid_1 + N_D_pid_1 * N_R_pid_1)/norm_1
  gen P_diff_2 = 1 - (Y_D_pid_2 * Y_R_pid_2 + N_D_pid_2 * N_R_pid_2 + U_D_pid_2 * U_R_pid_2)/norm_2

  sum P_diff_1
  local P_diff_1_lbm = r(mean)
  sum P_diff_2
  local P_diff_2_lbm = r(mean)

  
  
  
  /* Table B.5: Other Measures Comparing Democratic and Republican Voters */
quietly {
  capture log close
  log using "`PATH_TBL'/table_voting_A2.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Other Measures Comparing Democratic and Republican Voters} "
  noisily display "\label{table_voting_A2} "
  noisily display "\begin{tabular}{l c c c} "
  noisily display "\toprule\toprule "
  noisily display "               &              & Top 2 Plus  & All      \\ "
  noisily display "Contest Type   & Top 2 Only   & Roll-off    & Choices  \\ "
  noisily display "\midrule "

  noisily display "\multicolumn{4}{l}{\emph{Absolute Partisan Differences}} \\ [.05in] "
  noisily display "\midrule "
  noisily display "State Partisan Offices     & " %5.3f `diff_abs_1_spo' " & " %5.3f `diff_abs_2_spo' " & " %5.3f `diff_abs_3_spo' " \\ "
  noisily display "Local Partisan Offices     & " %5.3f `diff_abs_1_lpo' " & " %5.3f `diff_abs_2_lpo' " & " %5.3f `diff_abs_3_lpo' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices  & " %5.3f `diff_abs_1_sno' " & " %5.3f `diff_abs_2_sno' " & " %5.3f `diff_abs_3_sno' " \\ "
  noisily display "Local Nonpartisan Offices  & " %5.3f `diff_abs_1_lno' " & " %5.3f `diff_abs_2_lno' " & " %5.3f `diff_abs_3_lno' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures      & " %5.3f `diff_abs_1_sbm' " & " %5.3f `diff_abs_2_sbm' " & "                      " \\ "
  noisily display "Local Ballot Measures      & " %5.3f `diff_abs_1_lbm' " & " %5.3f `diff_abs_2_lbm' " & "                      " \\ "

  noisily display "\midrule\midrule"
  noisily display "\multicolumn{4}{l}{\emph{Squared Partisan Differences}} \\ [.05in] "
  noisily display "\midrule "
  noisily display "State Partisan Offices     & " %5.3f `diff_sq_1_spo'  " & " %5.3f `diff_sq_2_spo'  " & " %5.3f `diff_sq_3_spo' " \\ "
  noisily display "Local Partisan Offices     & " %5.3f `diff_sq_1_lpo'  " & " %5.3f `diff_sq_2_lpo'  " & " %5.3f `diff_sq_3_lpo' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices  & " %5.3f `diff_sq_1_sno'  " & " %5.3f `diff_sq_2_sno'  " & " %5.3f `diff_sq_3_sno' " \\ "
  noisily display "Local Nonpartisan Offices  & " %5.3f `diff_sq_1_lno'  " & " %5.3f `diff_sq_2_lno'  " & " %5.3f `diff_sq_3_lno' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures      & " %5.3f `diff_sq_1_sbm'  " & " %5.3f `diff_sq_2_sbm'  " & "                      " \\ "
  noisily display "Local Ballot Measures      & " %5.3f `diff_sq_1_lbm'  " & " %5.3f `diff_sq_2_lbm'  " & "                      " \\ "

  noisily display "\midrule\midrule"
  noisily display "\multicolumn{4}{l}{\emph{Normalized Probability of Different Choices}} \\ [.05in] "
  noisily display "\midrule "
  noisily display "State Partisan Offices     & " %5.3f `P_diff_1_spo'   " & " %5.3f `P_diff_2_spo'   " & " %5.3f `P_diff_3_spo' " \\ "
  noisily display "Local Partisan Offices     & " %5.3f `P_diff_1_lpo'   " & " %5.3f `P_diff_2_lpo'   " & " %5.3f `P_diff_3_lpo' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices  & " %5.3f `P_diff_1_sno'   " & " %5.3f `P_diff_2_sno'   " & " %5.3f `P_diff_3_sno' " \\ "
  noisily display "Local Nonpartisan Offices  & " %5.3f `P_diff_1_lno'   " & " %5.3f `P_diff_2_lno'   " & " %5.3f `P_diff_3_lno' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures      & " %5.3f `P_diff_1_sbm'   " & " %5.3f `P_diff_2_sbm'   " & "                      " \\ "
  noisily display "Local Ballot Measures      & " %5.3f `P_diff_1_lbm'   " & " %5.3f `P_diff_2_lbm'   " & "                      " \\ "

  noisily display "\bottomrule "
  noisily display "\end{tabular} "
  noisily display "\begin{tablenotes} "
  noisily display "\footnotesize \item All Choices includes all candidates who received at least 1\% of the total votes. "
  noisily display "\end{tablenotes} "
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}
